package com.h3c.nem.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.h3c.nem.dto.TbTeacherCertificationInfoDTO;
import com.h3c.nem.entity.SdDept;
import com.h3c.nem.entity.TbTeacherCertificationInfo;
import com.h3c.nem.mapper.SdDeptMapper;
import com.h3c.nem.mapper.TbTeacherCertificationInfoMapper;
import com.h3c.nem.service.CommService;
import com.h3c.nem.service.ITbTeacherService;
import com.h3c.nem.service.TbTeacherCertificationInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: yys44482
 * @Date: 2022/7/27
 */
@Service
public class TbTeacherCertificationInfoServiceImpl extends ServiceImpl<TbTeacherCertificationInfoMapper, TbTeacherCertificationInfo> implements TbTeacherCertificationInfoService {

    @Autowired
    TbTeacherCertificationInfoMapper tbTeacherCertificationInfoMapper;

    @Resource
    private ITbTeacherService tbTeacherService;

    @Autowired
    SdDeptMapper sdDeptMapper;

    @Autowired
    CommService commService;

    @Override
    public List<TbTeacherCertificationInfoDTO> queryTbTeacherCertificationInfo(TbTeacherCertificationInfoDTO dto) {
        List<String> deptIdList = new ArrayList<>();
        if (!StringUtils.isEmpty(dto.getDeptId())) {
            deptIdList.addAll( Arrays.asList(dto.getDeptId().split(",")));
        }
        if(deptIdList.size()==0){
            return new ArrayList<TbTeacherCertificationInfoDTO>();
        }
//        List<String> deptIds = null;
//        SdDept dept = dto.getDept();
        //主管管理的部门集合
        List<SdDept> deptList = dto.getDeptList();
        List<Integer> deptIds = deptList.stream().map(SdDept::getDeptId).collect(Collectors.toList());
//        //主管管理的部门id集合
//        List<String> manageDeptIds = new ArrayList<>();
//        if (!CollectionUtils.isEmpty(deptIdList)){
//            for (SdDept sdDept : deptList) {
//                String deptId = sdDept.getDeptId().toString();
//                String deptCodeChain = sdDept.getDeptCodeChain();
//                deptIds = new ArrayList<>(Arrays.asList(deptCodeChain.split("/")));
//                Iterator<String> iterator = deptIds.iterator();
//                //获取部门主管管理的部门id集合
//                while (iterator.hasNext()){
//                    String id = iterator.next();
//                    if (!id.equals(deptId)){
//                        iterator.remove();
//                    }else{
//                        break;
//                    }
//                    manageDeptIds.addAll(deptIds);
//                    manageDeptIds = manageDeptIds.stream().distinct().collect(Collectors.toList());
//                }
//            }
//        }else{
//            return null;
//        }
        //取交集
//        deptIdList = Lists.newArrayList(CollectionUtil.intersection(deptIdList, manageDeptIds));
        List<TbTeacherCertificationInfo> list = tbTeacherCertificationInfoMapper.queryListByFuzzy(dto, deptIds);
        List<TbTeacherCertificationInfoDTO> tbTeacherCertificationInfoDTOS = JSONArray.parseArray(JSONArray.toJSONString(list), TbTeacherCertificationInfoDTO.class);
        for (TbTeacherCertificationInfoDTO tbTeacherCertificationInfoDTO : tbTeacherCertificationInfoDTOS) {
            if (!StringUtils.isEmpty(tbTeacherCertificationInfoDTO.getLast4kpi())) {
                String[] arr = tbTeacherCertificationInfoDTO.getLast4kpi().split(";");

                for (int i = 3; i >= 0; i--) {
                    if (arr.length > i) {
                        if (i == 3) {
                            tbTeacherCertificationInfoDTO.setLastKpi4(arr[i].split(":")[1]);
                        } else if (i == 2) {
                            tbTeacherCertificationInfoDTO.setLastKpi3(arr[i].split(":")[1]);
                        } else if (i == 1) {
                            tbTeacherCertificationInfoDTO.setLastKpi2(arr[i].split(":")[1]);
                        } else {
                            tbTeacherCertificationInfoDTO.setLastKpi1(arr[i].split(":")[1]);
                        }

                    }
                }
            }
            if (!StringUtils.isEmpty(tbTeacherCertificationInfoDTO.getLast4culture())) {
                String[] arr = tbTeacherCertificationInfoDTO.getLast4culture().split(";");
                for (int i = 3; i >= 0; i--) {
                    if (arr.length > i) {
                        if (i == 3) {
                            tbTeacherCertificationInfoDTO.setLastCul4(arr[i].split(":")[1]);
                        } else if (i == 2) {
                            tbTeacherCertificationInfoDTO.setLastCul3(arr[i].split(":")[1]);
                        } else if (i == 1) {
                            tbTeacherCertificationInfoDTO.setLastCul2(arr[i].split(":")[1]);
                        } else {
                            tbTeacherCertificationInfoDTO.setLastCul1(arr[i].split(":")[1]);
                        }
                    }
                }
            }
            if (tbTeacherCertificationInfoDTO.getDeptNameChain() != null) {
                String[] deptNameChianArr = tbTeacherCertificationInfoDTO.getDeptNameChain().split("/");
                int i = deptNameChianArr.length - 1;
                if (i == 1) {
                    tbTeacherCertificationInfoDTO.setDept1name(deptNameChianArr[1]);
                } else if (i == 2) {
                    tbTeacherCertificationInfoDTO.setDept1name(deptNameChianArr[1]);
                    tbTeacherCertificationInfoDTO.setDept2name(deptNameChianArr[2]);
                }
                if (i == 3) {
                    tbTeacherCertificationInfoDTO.setDept1name(deptNameChianArr[1]);
                    tbTeacherCertificationInfoDTO.setDept2name(deptNameChianArr[2]);
                    tbTeacherCertificationInfoDTO.setDept3name(deptNameChianArr[3]);
                }
                if (i == 4) {
                    tbTeacherCertificationInfoDTO.setDept1name(deptNameChianArr[1]);
                    tbTeacherCertificationInfoDTO.setDept2name(deptNameChianArr[2]);
                    tbTeacherCertificationInfoDTO.setDept3name(deptNameChianArr[3]);
                    tbTeacherCertificationInfoDTO.setDept4name(deptNameChianArr[4]);
                }
            }
        }
        return tbTeacherCertificationInfoDTOS;
    }


    @Override
    public void sendCert(TbTeacherCertificationInfoDTO paramsDTO) {
        tbTeacherService.instantCertificate(paramsDTO.getWorkerIdList());
        for (String workerId : paramsDTO.getWorkerIdList()) {
            tbTeacherCertificationInfoMapper.deleteById(workerId);
        }
    }
}
